package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfSubAccountList;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoXfSubAccountList;
import com.ocom.payhandle.service.XfSubAccountListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class XfSubAccountListServiceImpl extends ServiceImpl<IDaoXfSubAccountList, EntityXfSubAccountList> implements XfSubAccountListService {


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public EntityXfSubAccountList getUserSubAccount(Long companyId, Long pId, Integer subAccount) {
        QueryWrapper<EntityXfSubAccountList> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("p_id",pId);
        queryWrapper.eq("sub_account",subAccount);
        EntityXfSubAccountList entityXfSubAccountList=this.getOne(queryWrapper);
        if(entityXfSubAccountList==null){
            EntityXfSubAccountList entityXfSubAccountList1 =new EntityXfSubAccountList();
            entityXfSubAccountList1.setComId(companyId);
            entityXfSubAccountList1.setSubAccount(subAccount);
            entityXfSubAccountList1.setPId(pId);
            entityXfSubAccountList1.setAccountRechargeMoney(0);
            entityXfSubAccountList1.setAccountSubsidyMoney(0);
            this.save(entityXfSubAccountList1);
            return  entityXfSubAccountList1;
        }
        return entityXfSubAccountList;
    }


    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public Boolean chageUserMoney(Long companyId , Long pId , Integer subAccount ,Integer money, Integer subMoney) {
        QueryWrapper<EntityXfSubAccountList> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("com_id", companyId );
        queryWrapper.eq("p_id",pId);
        queryWrapper.eq("sub_account",subAccount);
        EntityXfSubAccountList entityXfSubAccountList = this.getOne(queryWrapper);
        if(entityXfSubAccountList == null){
            throw new RuntimeException("充值账户不存在");
        }
        UpdateWrapper<EntityXfSubAccountList> updateWrapper= new UpdateWrapper<>();
        updateWrapper.eq("com_id", companyId );
        updateWrapper.eq("p_id",pId);
        updateWrapper.eq("sub_account",subAccount);
        updateWrapper.setSql("account_recharge_money = account_recharge_money + "+money);
        updateWrapper.setSql("account_subsidy_money = account_subsidy_money + "+subMoney);
        boolean b = this.update(updateWrapper);
        if (!b) {
            throw new RuntimeException("充值失败");
        }
        return true;
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public EntityXfSubAccountList chageUserMoney2(Long companyId , Long pId , Integer subAccount ,Integer money, Integer subMoney) {
        QueryWrapper<EntityXfSubAccountList> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("com_id", companyId );
        queryWrapper.eq("p_id",pId);
        queryWrapper.eq("sub_account",subAccount);
        EntityXfSubAccountList entityXfSubAccountList = this.getOne(queryWrapper);
        if(entityXfSubAccountList == null){
            throw new RuntimeException("充值账户不存在");
        }
        UpdateWrapper<EntityXfSubAccountList> updateWrapper= new UpdateWrapper<>();
        updateWrapper.eq("com_id", companyId );
        updateWrapper.eq("p_id",pId);
        updateWrapper.eq("sub_account",subAccount);
        updateWrapper.setSql("account_recharge_money = account_recharge_money + "+money);
        updateWrapper.setSql("account_subsidy_money = account_subsidy_money + "+subMoney);

        updateWrapper.last("and 0 <= (account_recharge_money + " + money + ") and 0 <= (account_subsidy_money + " + subMoney + ")");
        boolean b = this.update(updateWrapper);
        if(!b){
            throw new RuntimeException("充值失败");
        }
        return this.getOne(queryWrapper);
    }
}
